package com.gqr.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * Copyright (C), 2020-2024,gqr
 * FileName:AtomicIntegerFieldUpdaterDemo
 * author:guqingrong
 * Date:2024-05-14 21:32
 * Description:演示AtomicIntegerFieldUpdater的用法
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class AtomicIntegerFieldUpdaterDemo implements Runnable{
    static Candidate tom;
    static Candidate peter;

    public static AtomicIntegerFieldUpdater<Candidate> scoreUpdater = AtomicIntegerFieldUpdater.newUpdater(Candidate.class,"score");

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            peter.score++;
            scoreUpdater.getAndIncrement(tom);
        }
    }

    public static class Candidate{
        volatile int score;
    }


    public static void main(String[] args) throws InterruptedException {
        tom = new Candidate();
        peter = new Candidate();
        AtomicIntegerFieldUpdaterDemo r = new AtomicIntegerFieldUpdaterDemo();
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("普通变量"+peter.score);
        System.out.println("升级后的结果"+tom.score);
    }
}
